using Newtonsoft.Json;
using System.Collections.Generic;

namespace BanZuiCloudControlAgent.Core.Mqtt.Models
{
    /// <summary>
    /// 错误上报消息负载模型
    /// </summary>
    public class ErrorReportPayloadModel
    {
        /// <summary>
        /// 错误类型
        /// </summary>
        [JsonProperty("errorType")]
        public string ErrorType { get; set; }

        /// <summary>
        /// 错误级别
        /// </summary>
        [JsonProperty("errorLevel")]
        public int ErrorLevel { get; set; }

        /// <summary>
        /// 错误代码
        /// </summary>
        [JsonProperty("errorCode")]
        public string ErrorCode { get; set; }

        /// <summary>
        /// 错误消息
        /// </summary>
        [JsonProperty("errorMessage")]
        public string ErrorMessage { get; set; }

        /// <summary>
        /// 错误详情
        /// </summary>
        [JsonProperty("errorDetail")]
        public string ErrorDetail { get; set; }

        /// <summary>
        /// 任务ID
        /// </summary>
        [JsonProperty("taskId")]
        public string TaskId { get; set; }

        /// <summary>
        /// QQ账号ID
        /// </summary>
        [JsonProperty("qqAccountId")]
        public string QQAccountId { get; set; }

        /// <summary>
        /// QQ号码
        /// </summary>
        [JsonProperty("qqNumber")]
        public string QQNumber { get; set; }

        /// <summary>
        /// 上下文信息
        /// </summary>
        [JsonProperty("context")]
        public Dictionary<string, object> Context { get; set; }
    }

    /// <summary>
    /// 错误上报消息模型
    /// </summary>
    public class ErrorReportModel : BaseMessageModel
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ErrorReportModel() : base()
        {
            MessageType = MessageTypeConstants.ERROR_REPORT;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="payload">错误上报负载</param>
        public ErrorReportModel(string deviceCode, ErrorReportPayloadModel payload)
            : base(MessageTypeConstants.ERROR_REPORT, deviceCode, payload)
        {
        }

        /// <summary>
        /// 获取错误上报负载
        /// </summary>
        /// <returns>错误上报负载</returns>
        public ErrorReportPayloadModel GetPayload()
        {
            return Payload as ErrorReportPayloadModel;
        }

        /// <summary>
        /// 创建任务错误上报消息
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="taskId">任务ID</param>
        /// <param name="errorCode">错误代码</param>
        /// <param name="errorMessage">错误消息</param>
        /// <param name="errorDetail">错误详情</param>
        /// <param name="context">上下文信息</param>
        /// <returns>错误上报消息模型</returns>
        public static ErrorReportModel CreateTaskErrorReport(
            string deviceCode,
            string taskId,
            string errorCode,
            string errorMessage,
            string errorDetail = null,
            Dictionary<string, object> context = null)
        {
            var payload = new ErrorReportPayloadModel
            {
                ErrorType = ErrorTypeConstants.TASK_ERROR,
                ErrorLevel = 2,
                ErrorCode = errorCode,
                ErrorMessage = errorMessage,
                ErrorDetail = errorDetail,
                TaskId = taskId,
                Context = context ?? new Dictionary<string, object>()
            };

            return new ErrorReportModel(deviceCode, payload);
        }

        /// <summary>
        /// 创建QQ错误上报消息
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="qqNumber">QQ号码</param>
        /// <param name="errorCode">错误代码</param>
        /// <param name="errorMessage">错误消息</param>
        /// <param name="errorDetail">错误详情</param>
        /// <param name="context">上下文信息</param>
        /// <returns>错误上报消息模型</returns>
        public static ErrorReportModel CreateQQErrorReport(
            string deviceCode,
            string qqNumber,
            string errorCode,
            string errorMessage,
            string errorDetail = null,
            Dictionary<string, object> context = null)
        {
            var payload = new ErrorReportPayloadModel
            {
                ErrorType = ErrorTypeConstants.LOGIN_ERROR,
                ErrorLevel = 2,
                ErrorCode = errorCode,
                ErrorMessage = errorMessage,
                ErrorDetail = errorDetail,
                QQNumber = qqNumber,
                Context = context ?? new Dictionary<string, object>()
            };

            return new ErrorReportModel(deviceCode, payload);
        }

        /// <summary>
        /// 创建系统错误上报消息
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="errorCode">错误代码</param>
        /// <param name="errorMessage">错误消息</param>
        /// <param name="errorDetail">错误详情</param>
        /// <param name="errorLevel">错误级别</param>
        /// <param name="context">上下文信息</param>
        /// <returns>错误上报消息模型</returns>
        public static ErrorReportModel CreateSystemErrorReport(
            string deviceCode,
            string errorCode,
            string errorMessage,
            string errorDetail = null,
            int errorLevel = 2,
            Dictionary<string, object> context = null)
        {
            var payload = new ErrorReportPayloadModel
            {
                ErrorType = ErrorTypeConstants.SYSTEM_ERROR,
                ErrorLevel = errorLevel,
                ErrorCode = errorCode,
                ErrorMessage = errorMessage,
                ErrorDetail = errorDetail,
                Context = context ?? new Dictionary<string, object>()
            };

            return new ErrorReportModel(deviceCode, payload);
        }

        /// <summary>
        /// 创建网络错误上报消息
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="errorCode">错误代码</param>
        /// <param name="errorMessage">错误消息</param>
        /// <param name="errorDetail">错误详情</param>
        /// <param name="context">上下文信息</param>
        /// <returns>错误上报消息模型</returns>
        public static ErrorReportModel CreateNetworkErrorReport(
            string deviceCode,
            string errorCode,
            string errorMessage,
            string errorDetail = null,
            Dictionary<string, object> context = null)
        {
            var payload = new ErrorReportPayloadModel
            {
                ErrorType = ErrorTypeConstants.NETWORK_ERROR,
                ErrorLevel = 2,
                ErrorCode = errorCode,
                ErrorMessage = errorMessage,
                ErrorDetail = errorDetail,
                Context = context ?? new Dictionary<string, object>()
            };

            return new ErrorReportModel(deviceCode, payload);
        }
    }
} 